<?php

/**
 * @author phuocndt
 * @copyright 2010
 */
 
function getByParent($parent_id)
{
	$categories_query = tep_db_query("select * from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.parent_id = '" . (int)$parent_id . "' and c.categories_id = cd.categories_id order by sort_order, cd.categories_name");
	$arr = array();
	while($row=mysql_fetch_object($categories_query))
	{
		$arr[]= (array)$row;
	}
	return $arr;
}
function countSubItem($parent_id)
{
    $categories_query = tep_db_query("select count(*) as count from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.parent_id = '" . (int)$parent_id . "' and c.categories_id = cd.categories_id order by sort_order, cd.categories_name");
	$row=mysql_fetch_array($categories_query);
	//echo $row['count'];
    return $row['count'];    
}
function getSubMenuString($parent)
{
	$items = getByParent($parent);
	$strMenu =  '';
	foreach($items as $item)
	{
		if(countSubItem($item["categories_id"])>0)
		{
			$strMenu.="<li><a style=\"font-weight: bold;color:#80C64B;\" href=\"index.php?cPath=".$item["categories_id"]."\" title=\"$item[categories_name]\">$item[categories_name]</a>";
			$strMenu.=  '<ul>';
			$strMenu.= getSubMenuString($item["categories_id"]);
			$strMenu.=  '</ul></li>';				
		}
		else				
			$strMenu.="<li><a style=\"font-weight: bold;color:#80C64B;\" href=\"index.php?cPath=".$item["categories_id"]."\" title=\"$item[categories_name]\">$item[categories_name]</a></li>";				
	}
	return $strMenu;
}
function getMenuString()
{
	$arrTop = getByParent(0);
	$strMenu = '<div class="lmtop">Sản phẩm</div><ul class="sf-menu sf-vertical lmmid">';
    //$strMenu = '';
	foreach($arrTop as $item)
	{			
		$strMenu.= "<li><a style=\"font-weight: bold;width: 173px;color:#80C64B;\" href=\"index.php?cPath=".$item["categories_id"]."\" title=\"$item[categories_name]\">";			
		if(countSubItem($item["categories_id"])>0)
		{				
			$strMenu.= "$item[categories_name]</a>";
			$strMenu.= '<ul>';
			$strMenu.= getSubMenuString($item["categories_id"]);
			$strMenu.= '</ul></li>';
		}
		else                                       
			$strMenu.="$item[categories_name]</a></li>";				
	}
	$strMenu.='</ul>';
	return $strMenu;
}
echo getMenuString();
?>